
touch "$master_file_path/data/intermediate_data/Alternative_Gs/Alternative_Gs_G108.dta", replace

cd "$master_file_path/data/intermediate_data/Engel_curves/Engel_V1/"

forvalues G=1(1)108 {

capture {

clear
use Short_g25_rshare_V1_DM_G108_`G'_bs0.dta

egen group=group(market_id)
sum group
if (`r(max)'!=407) break
drop group

gen decile_dist=decile/10
rename sector sector0
gen sector="Rural"

drop sector
rename sector0 sector


keep if r43count_hh>=100  & r55count_hh>=100


foreach var of varlist W*_lnmpcew P*lp_owbwg25*_9901   {

gen V`var'=exp(`var')

}
*

foreach initial in 43 55 { 
foreach final in 43  55 {


if `initial'!=`final' {

bysort decile: egen Y_`initial'_`final'_x = wtmean( VW`initial'`final'_lnmpcew), weight(r4355total_wt)
gen Y_`initial'`final'_decile=100*(Y_`initial'_`final'_x-1)

bysort decile: egen ZY_`initial'_`final'_x = wtmean( W`initial'`final'_lnmpcew ), weight(r4355total_wt)
gen ZY_`initial'`final'_decile=100*(exp(ZY_`initial'_`final'_x)-1)
}
*

if `initial'!=`final' {

bysort decile: egen E_`initial'_`final'_x = wtmean( VP`initial'`final'lp_owbwg25cen_9901), weight(r4355total_wt)  // this is in ratio
bysort decile: egen ZE_`initial'_`final'_x = wtmean( P`initial'`final'lp_owbwg25cen_9901), weight(r4355total_wt)  // this is log change

gen E_`initial'`final'_decile=100*(E_`initial'_`final'_x-1)
gen ZE_`initial'`final'_decile=100*(exp(ZE_`initial'_`final'_x)-1)

gen WE_`initial'`final'_decile=Y_`initial'`final'_decile-E_`initial'`final'_decile
gen WZE_`initial'`final'_decile=ZY_`initial'`final'_decile-ZE_`initial'`final'_decile

}
}
}
*

egen tag=tag(decile)
keep if tag==1


gen G_groups=`G'

keep G_groups decile E_4355_decile ZE_4355_decile WE_4355_decile WZE_4355_decile E_5543_decile ZE_5543_decile WE_5543_decile WZE_5543_decile

compress

append using "$master_file_path/data/intermediate_data/Alternative_Gs/Alternative_Gs_G108.dta"

compress

save "$master_file_path/data/intermediate_data/Alternative_Gs/Alternative_Gs_G108.dta", replace
clear

}
}
*

*****************************************************************************************************************************************************************
*****************************************************************************************************************************************************************
*********************************************CREATE 4 PANELS FOR FIGURE 6****************************************************************************************
*****************************************************************************************************************************************************************
*****************************************************************************************************************************************************************


clear all

use "$master_file_path/data/intermediate_data/Alternative_Gs/Alternative_Gs_G108.dta",clear


sum ZE_5543_decile if G_groups==15 & decile==10
local baseline10=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==20
local baseline20=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==30
local baseline30=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==40
local baseline40=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==50
local baseline50=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==60
local baseline60=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==70
local baseline70=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==80
local baseline80=`r(mean)'
sum ZE_5543_decile if G_groups==15 & decile==90
local baseline90=`r(mean)'


sum ZE_5543_decile if decile==10
local baseline10_m=`r(mean)'
sum ZE_5543_decile if decile==20
local baseline20_m=`r(mean)'
sum ZE_5543_decile if decile==30
local baseline30_m=`r(mean)'
sum ZE_5543_decile if decile==40
local baseline40_m=`r(mean)'
sum ZE_5543_decile if decile==50
local baseline50_m=`r(mean)'
sum ZE_5543_decile if decile==60
local baseline60_m=`r(mean)'
sum ZE_5543_decile if decile==70
local baseline70_m=`r(mean)'
sum ZE_5543_decile if decile==80
local baseline80_m=`r(mean)'
sum ZE_5543_decile if decile==90
local baseline90_m=`r(mean)'


sum ZE_4355_decile if G_groups==15 & decile==10
local baseline10_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==20
local baseline20_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==30
local baseline30_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==40
local baseline40_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==50
local baseline50_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==60
local baseline60_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==70
local baseline70_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==80
local baseline80_0=`r(mean)'
sum ZE_4355_decile if G_groups==15 & decile==90
local baseline90_0=`r(mean)'


sum ZE_4355_decile if decile==10
local baseline10_m_0=`r(mean)'
sum ZE_4355_decile if decile==20
local baseline20_m_0=`r(mean)'
sum ZE_4355_decile if decile==30
local baseline30_m_0=`r(mean)'
sum ZE_4355_decile if decile==40
local baseline40_m_0=`r(mean)'
sum ZE_4355_decile if decile==50
local baseline50_m_0=`r(mean)'
sum ZE_4355_decile if decile==60
local baseline60_m_0=`r(mean)'
sum ZE_4355_decile if decile==70
local baseline70_m_0=`r(mean)'
sum ZE_4355_decile if decile==80
local baseline80_m_0=`r(mean)'
sum ZE_4355_decile if decile==90
local baseline90_m_0=`r(mean)'



***************************************************************************************************************************************
***************************************************************************************************************************************
***************************2 PANELS WITH DISTRIBUTIONS*********************************************************************************
***************************************************************************************************************************************
***************************************************************************************************************************************


graph set window fontface "Times New Roman"

twoway (kdensity ZE_5543_decile if decile==20, color(gs13) xline(`baseline20', lcolor(gs13))) ///
(kdensity ZE_5543_decile if decile==20, color(gs13) lp(dash) xline(`baseline20_m', lcolor(gs13) lpattern(dash))) ///
(kdensity ZE_5543_decile if decile==50, color(gs10) xline(`baseline50', lcolor(gs10))) ///
(kdensity ZE_5543_decile if decile==50, color(gs10) lp(dash) xline(`baseline50_m', lcolor(gs10) lpattern(dash))) ///
(kdensity ZE_5543_decile if decile==80, color(gs0) xline(`baseline80', lcolor(gs0))) ///
(kdensity ZE_5543_decile if decile==80, color(gs0) lp(dash) xline(`baseline80_m', lcolor(gs0) lpattern(dash))), ///
xtitle("AFFG Price Index (P{superscript:1})") title("") ytitle("Density") ///
legend(order(1 2 3 4 5 6) label(1 "2nd Decile (Baseline)")  label(2 "2nd Decile (Mean)") label(3 "5th Decile (Baseline)") label(4 "5th Decile (Mean)")  label(5 "8th Decile (Baseline)") label(6 "8th Decile (Mean)") ///
region(lcolor(white) fcolor(white)) span row(3)) graphregion(lcolor(white) fcolor(white)) title("Panel D: P{superscript:1}")

graph export "$master_file_path/output/Figure6_D.pdf", replace



graph set window fontface "Times New Roman"

twoway (kdensity ZE_4355_decile if decile==20, color(gs13) xline(`baseline20_0', lcolor(gs13))) ///
(kdensity ZE_4355_decile if decile==20, color(gs13) lp(dash) xline(`baseline20_m_0', lcolor(gs13) lpattern(dash))) ///
(kdensity ZE_4355_decile if decile==50, color(gs10) xline(`baseline50_0', lcolor(gs10))) ///
(kdensity ZE_4355_decile if decile==50, color(gs10) lp(dash) xline(`baseline50_m_0', lcolor(gs10) lpattern(dash))) ///
(kdensity ZE_4355_decile if decile==80, color(gs0) xline(`baseline80_0', lcolor(gs0))) ///
(kdensity ZE_4355_decile if decile==80, color(gs0) lp(dash) xline(`baseline80_m_0', lcolor(gs0) lpattern(dash))), ///
xtitle("AFFG Price Index (P{superscript:0})") title("") ytitle("Density") ///
legend(off) ///
graphregion(lcolor(white) fcolor(white)) title("Panel B: P{superscript:0}")

graph export  "$master_file_path/output/Figure6_B.pdf", replace



***************************************************************************************************************************************
***************************************************************************************************************************************
***************************2 PANELS PLOTTING MEANS, CONFIDENCE INTERVALS AND OUR BASELINE ESITMATE*************************************
***************************************************************************************************************************************
***************************************************************************************************************************************


gen ZE_5543_decile_sd=ZE_5543_decile
gen ZE_5543_decile_m=ZE_5543_decile
gen ZE_5543_decile_base=ZE_5543_decile if G_groups==15

gen ZE_4355_decile_sd=ZE_4355_decile
gen ZE_4355_decile_m=ZE_4355_decile
gen ZE_4355_decile_base=ZE_4355_decile if G_groups==15


collapse (mean) ZE_5543_decile_m ZE_5543_decile_base ZE_4355_decile_m ZE_4355_decile_base (sd) ZE_5543_decile_sd ZE_4355_decile_sd, by(decile)

gen lbound=ZE_5543_decile_m-1.96*ZE_5543_decile_sd
gen ubound=ZE_5543_decile_m+1.96*ZE_5543_decile_sd

gen lbound_0=ZE_4355_decile_m-1.96*ZE_4355_decile_sd
gen ubound_0=ZE_4355_decile_m+1.96*ZE_4355_decile_sd

graph set window fontface "Times New Roman"

twoway (rcap lbound ubound decile) (scatter ZE_5543_decile_m decile, m(Oh)) (scatter ZE_5543_decile_base decile), ///
xtitle("Deciles of Income Distribution") title("") ytitle("AFFG Price Index (P{superscript:1})") ///
legend(order(2 3) label(2 "Mean Across Alternative Gs")  label(3 "Baseline Estimate") ///
region(lcolor(white) fcolor(white)) span row(1)) graphregion(lcolor(white) fcolor(white)) xlabel(10(10)90) title("Panel C: P{superscript:1}")

graph export "$master_file_path/output/Figure6_C.pdf", replace


graph set window fontface "Times New Roman"

twoway (rcap lbound_0 ubound_0 decile) (scatter ZE_4355_decile_m decile, m(Oh)) (scatter ZE_4355_decile_base decile), ///
xtitle("Deciles of Income Distribution") title("") ytitle("AFFG Price Index (P{superscript:0})") ///
legend(off) ///
graphregion(lcolor(white) fcolor(white)) xlabel(10(10)90) title("Panel A: P{superscript:0}") ylabel(120(20)180)

graph export "$master_file_path/output/Figure6_A.pdf", replace

clear all

